package com.itheima.tlias84.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias84.config.StudentConfig;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("yml")
public class TestReadYmlController {

    /**
     *只能读8种基本数据类型外加数组，集合读不了
     */
//    @Value("${student.name}")
//    String name;
//    @Value("${student.age}")
//    Integer age;
//    @Value("${student.subject}")
//    String[] subject;
//    @Value("${student.teachers}")
//    List<String> teathers;
    @Resource
    StudentConfig studentConfig;
    @Resource
    Environment env;
    @GetMapping
    public ResultUtil readCustomConfigByYml(){
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name",studentConfig.getName());
        jsonObject.put("age",studentConfig.getAge());
        jsonObject.put("subject",studentConfig.getSubject());
        jsonObject.put("teathers",studentConfig.getTeachers());
        jsonObject.put("family",studentConfig.getFamily());
        jsonObject.put("a","----------------------------------------");
        String[] profiles = env.getActiveProfiles();
        jsonObject.put("profiles",profiles);
        String property = env.getProperty("server.port");
        jsonObject.put("port",property);
        return ResultUtil.success(jsonObject);

    }
}
